import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-06-01
 * Time: 10:40
 */
public class MySingleList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    //头节点
    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }

    //遍历单链表
    public void show() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //得到单链表的长度 --》 链表中节点的个数
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        int size = size();
        if(index < 0 || index > size) {
            throw new IndexOfBounds("Index输入不合法, 此时, index : " + index);
        }
        if(0 == index) {
            addFirst(data);
            return;
        }
        if(size == index) {
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }
    // 保存插入节点的前驱节点
    public ListNode findIndex(int index) {
        ListNode cur = head;
        while(index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode prev = searchPrev(key);
        if(prev == null) {
            return;
        }
        ListNode del = prev.next;
        prev.next = del.next;

    }
    // 保存被删除的前驱节点
    public ListNode searchPrev(int key) {
        ListNode prev = head;
        while(prev.next != null) {
            if(prev.next.val == key) {
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
            }else {
                prev = prev.next;
            }
            cur = cur.next;
        }
        if(head.val == key) {
            head = head.next;
        }
    }
    // 清空链表
    public void clear() {
        this.head = null;
    }
    // 逆置单链表
    public ListNode reverseList() {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    // 求中间节点
    public ListNode middleNode(ListNode head) {
        if(head == null) {
            return null;
        }
        if(head.next == null) {
            return head;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    // 输入一个链表，输出该链表中倒数第k个结点。
    public ListNode findKthToTail(int k) {
        int size = size();
        if(k < 1 || k > size) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(k - 1 != 0) {
            fast = fast.next;
            k--;
        }
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
    //合并两个有序链表
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null && list2 ==null) {
            return null;
        }
        if(list1 == null) {
            return list2;
        }
        if(list2 == null) {
            return list1;
        }
        ListNode newHead = new ListNode(-1);
        ListNode tmpH = newHead;
        while(list1 != null && list2 != null) {
            if(list1.val <= list2.val) {
                tmpH.next = list1;
                tmpH = list1;
                list1 = list1.next;
            }else {
                tmpH.next = list2;
                tmpH = list2;
                list2 = list2.next;
            }
        }
        if(list1 == null) {
            tmpH.next = list2;
        }
        if(list2 == null) {
            tmpH.next = list1;
        }
        return newHead.next;
    }
    // 链表分割
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;
        while(cur != null) {
            if(cur.val < x) {
                if(bs == null) {
                    bs = cur;
                    be = cur;

                }else {
                    be.next = cur;
                    be = cur;

                }
            }else {
                if(as == null) {
                    as = cur;
                    ae = cur;

                }else {
                    ae.next = cur;
                    ae = cur;
                }
            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }
    // 判断回文串
    public boolean chkPalindrome() {
            // 找中间节点
            ListNode fast = head;
            ListNode slow = head;
            while(fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            // 逆置单链表
            ListNode cur = slow.next;
            slow.next = null;
            while(cur != null) {
                ListNode curNext = cur.next;
                cur.next = slow;
                slow = cur;
                cur = curNext;
            }
            // 判断回文串
            while(head != slow) {
                // 判断奇数个节点
                if(head.val != slow.val) {
                    return false;
                }else {
                    // 判断偶数个节点的情况
                    if(head.next == slow) {
                        return true;
                    }
                    head = head.next;
                    slow = slow.next;
                }
            }
            return true;
    }
    // 求两个链表的公共节点
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pLong = headA; //假设A链表长
        ListNode pShort = headB;
        // 求链表的长度
        int len1 = 0;
        while(pLong != null) {
            len1++;
            pLong = pLong.next;
        }
        int len2 = 0;
        while(pShort != null) {
            len2++;
            pShort = pShort.next;
        }
        pLong = headA;
        pShort = headB;
        // 求差值步数
        int len = len1 - len2;
        if(len < 0) {
            len = len2 - len1;
            pLong = headB;
            pShort = headA;
        }
        // 长的链表先走len步
        while(len != 0) {
            pLong = pLong.next;
            len--;
        }
        // 同时走
        while(pLong != pShort) {
            pLong = pLong.next;
            pShort = pShort.next;
        }
        return pLong;
    }
    // 环型链表
    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }

        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }
    // 环形链表
    public ListNode detectCycle(ListNode head) {
        if(head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null) {
            return null;
        }
        if(fast.next == null) {
            return null;
        }
        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    public void show3(ListNode head) {
        if(head == null) {
            return;
        }
        if(head.next == null) {
            System.out.println(head.val);
            return;
        }
        show3(head.next);
        System.out.println(head.val);
    }

    public void show4() {
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        // 依次出栈
        while (!stack.isEmpty()) {
            ListNode tmp = stack.pop();
            System.out.println(tmp.val);
        }
    }

    // 递归方式
    void printList1(ListNode head){
        if(null != head){
            printList1(head.next);
            System.out.print(head.val + " ");
        }
    }
    // 循环方式
    void printList2(ListNode head){
        if(null == head){
            return;
        }
        Stack<ListNode> s = new Stack<>();
    // 将链表中的结点保存在栈中
        ListNode cur = head;
        while(null != cur){
            s.push(cur);
            cur = cur.next;
        }
    // 将栈中的元素出栈
        while(!s.empty()){
            System.out.print(s.pop().val + " ");
        }
    }

}
